//
//  CameraView.swift
//  skincare
//
//  Created by whu125 on 2023/4/24.
//
import UIKit
import AVFoundation
import SwiftUI

struct CameraView: View {
    @State var policy = OssPolicy(OSSAccessKeyId: "", policy: "", Signature: "", dir: "", host: "", expire: "")

    @State private var image: UIImage?
    @State private var showImagePicker: Bool = false
    func handlePickedImage(_ image: UIImage?) {
            self.image = image
           print("图片选择完毕")
        //   print(image)
        let filename=getFileName()
        camera_provider.request(.getOSSUrl)
        { result in
            switch result {
            case let .success(response):
                do {
                
                    let responseData = response.data
                    
                // 将JSON对象解码为自定义的数据模型
                //方法：根据返回的数据类型先在Model写对应的struct
                    let decodedObject = try JSONDecoder().decode(OssPolicy.self, from: responseData)
                    policy=decodedObject
                    
                    
                } catch let error {
                    print(error)
                }
            case let .failure(error):
                print(error)
            }
        }
           showImagePicker=false
        }
    
    var body: some View {
        
        
        GeometryReader { geometry in
            VStack(alignment: .center){
                
                
                VStack(alignment: .leading) {
                    ZStack {
                        Rectangle()
                            .fill(Color(hue: 0.376, saturation: 0.991, brightness: 0.134))
                            .frame(width: geometry.size.width,height: 90)
                            .cornerRadius(40) // 左下角和右下角圆角
                        HStack {
                            Button(action: {
                                // 点击左侧按钮的响应方法
                            }) {
                                Image(systemName: "person")
                                    .foregroundColor(.white)
                                    .padding([.top, .leading], 20.0)
                                    .font(.largeTitle)
                                
                            }
                            .padding(4.0)
                            Spacer() // 占用剩余空间
                        }
                        .padding(.horizontal)
                        
                    }
                    .edgesIgnoringSafeArea(.all) // 忽略安全区域
                }
                VStack(alignment:.center){
                    ZStack {
                        RoundedRectangle(cornerRadius: 20)
                            .fill(Color.gray)
                            .frame(width: geometry.size.width - 40,height: 100)
                        
                        RoundedRectangle(cornerRadius: 18)
                            .fill(Color.white)
                            .frame(width: geometry.size.width - 45,height: 95)
                        
                        VStack(alignment: .leading) {
                            Text("Hi~")
                                .font(.system(size: 30, weight: .bold))
                                .foregroundColor(.black)
                            Text("快来开始今日测一测吧")
                                .font(.system(size: 20, weight: .bold))
                                .foregroundColor(.black)
                        }
                    }
                    Button(action:{
                        self.showImagePicker = true
                    })
                    {
                        Image("takePhoto")
                            .padding(.top, 50)
                    }
                    .sheet(isPresented: $showImagePicker) {
                        //开发环境下暂时让相册选择 换成.camera
                        ImagePicker(sourceType: .photoLibrary, completionHandler: self.handlePickedImage)
                           }
                }
                .padding(.bottom, 120.0)
                
            }
            .frame(width:geometry.size.width)
        }
        
    }
}

struct CameraView_Previews: PreviewProvider {
    static var previews: some View {
        CameraView()
    }
}

